(function() {
    //请求分类标题数据  并渲染
    fetch('http://chst.vip:1234/api/getcategorytitle').then(body => body.json().then(res => {
        let result = res.result;
        console.log(result);
        let panelStr = '';
        result.forEach(async item => {
            let abc = await getDetails(item.titleId);
            panelStr += `
            <div class="panel"  date-index="${item.titleId}">
            <h2>
            <a href="#" class="panelTitle">${item.title}</a>
            <span>▼</span>
            </h2>
            </div>
            <div class="son" date-index="${item.titleId}">
            <ul class="panelCentent">
                ${abc}
            </ul>
            </div>
            `
            $('.box').html(panelStr);
        });
        async function getDetails(index) {
            index = index - 0;
            let detailStr = ``;
            await fetch('http://chst.vip:1234/api/getcategory?titleid=' + index).then(body => body.json()).then(res => {
                let result = res.result;
                // console.log(result);
                result.forEach(item => {
                    detailStr += `
                    <li date-index="${item.categoryId}"><a href="#">${item.category}</a></li>
                `
                });
            });
            return detailStr
        };

    }));
    //点击事件
    $('.box').on('click', '.panel', function() {
        $(this).next().stop().slideToggle(200);
        $(this).next().on('click', 'li', function() {
            let categoryId = $(this).attr('date-index');
            let title = $(this).text();
            location.href = '../pages/bijia.html?title=' + title + '&ategoryid=' + categoryId;
        })
    });
})()